package com.momo.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ：朱博
 * @description：实现发送数量拦截器
 * @date ：2021/3/28 13:36
 */
public class CounterInterceptor implements ProducerInterceptor<String,String> {
    /**
     * 不明白为甚不能用AtomicInteger
     */
     private AtomicInteger success = new AtomicInteger(0);
     private AtomicInteger error = new AtomicInteger(0);


    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        return record;
    }

    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        if(metadata != null){
            success.incrementAndGet();
        } else {
            error.incrementAndGet();

        }
    }

    @Override
    public void close() {
        System.out.println("success:" + success);
        System.out.println("error:" + error);
    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
